using System.Collections;
using System.Collections.Generic;
using System.Net.Sockets;
using Unity.Mathematics;
using UnityEngine;

public class gun : MonoBehaviour
{
    // Start is called before the first frame update

    public Rigidbody2D rocket;
    playerctrl player;
    AudioSource bazookaAD;
    public float speed = 20f;

    private Animator anim;
    void Start()
    {
        player = transform.parent.GetComponent<playerctrl>();
        bazookaAD=GetComponent<AudioSource>();
        anim = transform.parent.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Fire1")) 
        {
            if(anim!=null)
            {
                anim.SetTrigger("shoot");
            }
            bazookaAD.Play();
            if (player != null)
            {
                if (player.faceright)
                {
                    quaternion rotation = Quaternion.Euler(new Vector3(0, 0, 0));
                    Rigidbody2D rockectInstance = Instantiate(rocket, transform.position, rotation);
                    rockectInstance.velocity = new Vector2(speed, 0);
                }
                else
                {
                    quaternion rotation = Quaternion.Euler(new Vector3(0, 0, 180));
                    Rigidbody2D rockectInstance = Instantiate(rocket, transform.position, rotation);
                    rockectInstance.velocity = new Vector2(-speed, 0);
                }
            }
        }
    }
}
